package net.fei.scan.quick.adapter

import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context
import android.content.Intent
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.PopupMenu
import android.widget.Toast
import androidx.recyclerview.widget.RecyclerView
import com.google.gson.Gson
import net.fei.scan.quick.R
import net.fei.scan.quick.databinding.CreateItemBinding
import net.fei.scan.quick.databinding.MainItemBinding
import net.fei.scan.quick.db.DbUtils
import net.fei.scan.quick.entity.History
import net.fei.scan.quick.ui.AddActivity
import net.fei.scan.quick.ui.ContentActivity

/**
 *Create by Xianban
 *2023/11/9 14:22
 */

class MainAdapter(private var list:MutableList<History>,private val context:Context):
    RecyclerView.Adapter<MainAdapter.ViewHolder>() {
    private lateinit var history:History
    class ViewHolder(view:MainItemBinding):RecyclerView.ViewHolder(view.root) {
        val title=view.title
        val img=view.img
        val date=view.date
        val type=view.type
        val content=view.content
        val more=view.more
        val vi=view.view2
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val binding = MainItemBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        return ViewHolder(binding)
    }

    override fun getItemCount(): Int {
        if (list.size==0){
            list.add(0,History(1,context.getString(R.string.dfsjk),context.getString(R.string.kdsajf),null,"",R.drawable.scs))
        }
       return list.size
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
         history = list[position]
        if (position==list.size-1){
            holder.vi.visibility=View.GONE
        }
        holder.apply {
            title.text=history.title
            img.setBackgroundResource(history.image)
            date.text=history.currentTime
            type.text=history.type
            content.text=history.content
        }
        holder.more.setOnClickListener {showPopupMenu(it,history.id,position)
        }
        holder.itemView.setOnClickListener {
            val intent = Intent(context, ContentActivity::class.java)
            intent.putExtra("historys", Gson().toJson(history))
            context.startActivity(intent)
        }
    }
    private fun removeItem(position: Int) {
        list.removeAt(position)
        notifyItemRemoved(position)
    }
    private fun showPopupMenu(view: View,id:Long,position: Int) {
        val popupMenu = PopupMenu(view.context, view)
        popupMenu.menuInflater.inflate(R.menu.popup_menu, popupMenu.menu)
        popupMenu.setOnMenuItemClickListener { menuItem ->
            when (menuItem.itemId) {
                R.id.delete -> {
                    DbUtils(context).deleteData(id)
                    this.removeItem(position)
                    true
                }
                R.id.save->{
                    copyToClipboard(context,history.content)
                   true
                }
                else -> false

            }
        }
        popupMenu.show()
    }
    private fun copyToClipboard(context: Context, text: String, label: String = "Copied Text") {
        val clipboardManager = context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
        val clipData = ClipData.newPlainText(label, text)
        clipboardManager.setPrimaryClip(clipData)
        Toast.makeText(context, context.getString(R.string.sdjsdl), Toast.LENGTH_SHORT).show()
    }
}